package com.dhcc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.entity.SysDepartment;
import com.dhcc.mapper.ClientInstitutionRoleDepartmentMapper;
import com.dhcc.mapper.InstitutionRoleDepartmentPositionMapper;
import com.dhcc.mapper.SysDepartmentMapper;
import com.dhcc.service.SysDepartmentService;
import com.dhcc.utils.DepartmentTreeBuilder;
import com.dhcc.vo.SysDepartmentTree;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysDepartmentServiceImpl extends ServiceImpl<SysDepartmentMapper,SysDepartment> implements SysDepartmentService {
    @Resource
    private SysDepartmentMapper sysDepartmentMapper;
    @Resource
    private InstitutionRoleDepartmentPositionMapper institutionRoleDepartmentPositionMapper;
    @Resource
    private ClientInstitutionRoleDepartmentMapper clientInstitutionRoleDepartmentMapper;
    @Override
    public SysDepartment getDepatment(String departmentId) {
        return sysDepartmentMapper.getDepartment(departmentId);
    }

    @Override
    public List<String> getDepartmentList(String institutionId, String institutionRoleId) {
        //查询当前机构下的部门名称
        List<String> departmentIds= clientInstitutionRoleDepartmentMapper.getDepartmentIds(institutionId,institutionRoleId);
        List<String> departmentNameList=new ArrayList<>();
        for (String departmentId : departmentIds) {
            //通过部门编号查询部门名称
            SysDepartment department = sysDepartmentMapper.getDepartment(departmentId);
            departmentNameList.add(department.getDeptName());
        }
        return departmentNameList;
    }

    @Override
    public List<SysDepartmentTree> selectDepartmentListTree(String institutionId, String institutionRoleId) {
        //查询当前机构下的部门编号列表
        List<String> departmentIds= clientInstitutionRoleDepartmentMapper.getDepartmentIds(institutionId,institutionRoleId);
        List<SysDepartment> sysDepartmentList=new ArrayList<>();
        for (String departmentId : departmentIds) {
            //通过部门编号查询部门名称
            SysDepartment department = sysDepartmentMapper.getDepartment(departmentId);
            sysDepartmentList.add(department);
        }
        return DepartmentTreeBuilder.buildDeptTree(sysDepartmentList,null);
    }

    @Override
    public Integer addDepartment(SysDepartment sysDepartment) {
        return sysDepartmentMapper.addDepartment(sysDepartment);
    }

    @Override
    public List<SysDepartment> selectDepartmentList(String institutionId, String institutionRoleId) {
        //查询当前机构下的部门编号列表
        List<String> departmentIds= clientInstitutionRoleDepartmentMapper.getDepartmentIds(institutionId,institutionRoleId);
        List<SysDepartment> sysDepartmentList=new ArrayList<>();
        for (String departmentId : departmentIds) {
            //通过部门编号查询部门名称
            SysDepartment department = sysDepartmentMapper.getDepartment(departmentId);
            sysDepartmentList.add(department);
        }
        return sysDepartmentList;
    }

    @Override
    public List<SysDepartment> selectdepartmentListByFatherDeptId(String departmentId) {
        return sysDepartmentMapper.selectdepartmentListByFatherDeptId(departmentId);
    }

    @Override
    public Integer deleteDepartment(String departmentId) {
        return sysDepartmentMapper.updateIsDel(departmentId);
    }

    @Override
    public Integer updateDepartment(SysDepartment sysDepartment) {
        return sysDepartmentMapper.updateDepartment(sysDepartment);
    }
}
